
package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.saas.domain.dao.TagDao;
import com.xbongbong.saas.domain.entity.TagEntity;
import com.xbongbong.saas.enums.TagEnum;
import com.xbongbong.saas.enums.TagTypeEnum;
import com.xbongbong.saas.model.TagModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("tagModel")
public class TagModelImpl implements TagModel {

    @Resource
    private TagDao tagDao;

    @Override
    public Integer insert(TagEntity entity) {
        Integer now = DateUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        return tagDao.insert(entity);
    }

    @Override
    public Integer update(TagEntity entity) {
        entity.setUpdateTime(DateUtil.getInt());
        return tagDao.update(entity);
    }


    @Override
    public TagEntity getByKey(Integer key, String corpid) {
        return tagDao.getByKey(key, corpid);
    }

    @Override
    public List<TagEntity> findEntitys(Map<String, Object> param) {
        return tagDao.findEntitys(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return tagDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public TagEntity findReturnPublicTag(Integer code) {
        return getTagByTagType(code);
    }

    @Override
    public TagEntity findArchivedTag() {
        return getTagByTagType(TagTypeEnum.ARCHIVED.getCode());
    }

    @Override
    public TagEntity getTagByTagType(Integer tagType) {
        TagEnum tagEnum = TagEnum.getTagByTagType(tagType);
        if (tagEnum != null) {
            //目前只有退回公海池和归档标签，走enum减少sql查询  @author 魏荣杰  @date 2018/12/6 15:37
            TagEntity tagEntity = new TagEntity();
            tagEntity.setId(tagEnum.getId());
            tagEntity.setTagType(tagEnum.getTagType());
            tagEntity.setName(tagEnum.getName());
            tagEntity.setResource(tagEnum.getResource());
            tagEntity.setResourceType(tagEnum.getResourceType());
            tagEntity.setMemo(tagEnum.getMemo());
            return tagEntity;
        } else {
            Map<String, Object> params = new HashMap<>();
            params.put("corpid", '0');
            params.put("tagType", tagType);
            params.put("del", 0);
            params.put("columns", "id,tag_type,name,resource,resource_type,memo");
            params.put("limitNum", 1);
            List<TagEntity> tagList = findEntitys(params);
            return tagList.size() == 0 ? null : tagList.get(0);
        }
    }

    @Override
    public List<TagEntity> getTags(String corpid) {
        Map<String, Object> params = new HashMap<>();
        List<String> corpidIn = new ArrayList<>();
        corpidIn.add("0");
        if (!StringUtil.isEmpty(corpid)) {
            corpidIn.add(corpid);
        }
        params.put("corpidIn", corpidIn);
        params.put("status", 1);
        params.put("del", 0);
        return findEntitys(params);
    }

    @Override
    public Map<Integer, TagEntity> getTagsMap(String corpid) {
        List<TagEntity> tagList = this.getTags(corpid);
        assert tagList != null;
        Map<Integer, TagEntity> tagsMap = new HashMap<>();
        if (tagList.size() > 0) {
            for (TagEntity tag : tagList) {
                tagsMap.put(tag.getId(), tag);
            }
        }
        return tagsMap;
    }

}

